package com.firefly.admin.system.controller;

import com.firefly.admin.common.annotations.annotation.OperationLog;
import com.firefly.admin.common.enums.OperationModuleEnum;
import com.firefly.admin.common.enums.OperationTypeEnum;
import com.firefly.admin.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.firefly.admin.system.entity.FySystemMenu;
import com.firefly.admin.system.service.FySystemMenuService;
import com.firefly.admin.system.vo.MenuTree;
import com.firefly.admin.system.vo.SystemMenuQueryVo;
import com.firefly.admin.system.vo.SystemMenuStatusUpdateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "系统菜单管理接口API")
@RestController
@RequestMapping("/system/menu")
public class FySystemMenuController {
    @Autowired
    private FySystemMenuService fySystemMenuService;

    /**
     * 分页查询系统菜单接口
     *
     * @param systemMenuQueryVo 分页查询参数
     * @return 分页列表
     */
    @ApiOperation(value = "分页查询系统菜单接口", notes = "分页查询系统菜单接口", httpMethod = "GET")
    @PreAuthorize(value = "hasAuthority('system:menu')")
    @GetMapping("/list")
    public Result<List<FySystemMenu>> getSystemMenuList(SystemMenuQueryVo systemMenuQueryVo) {
        return fySystemMenuService.getSystemMenuList(systemMenuQueryVo);
    }

    /**
     * 获取系统菜单信息接口
     *
     * @param menuId 菜单ID
     * @return 菜单信息
     */
    @ApiOperation(value = "获取系统菜单信息接口", notes = "通过菜单ID获取菜单详细信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", required = true, value = "菜单ID", paramType = "path", dataType = "Long")
    })
    @GetMapping("/{menuId}")
    public Result<FySystemMenu> getSystemMenu(@PathVariable Long menuId) {
        return fySystemMenuService.getSystemMenu(menuId);
    }

    /**
     * 系统菜单信息新增接口
     *
     * @param fySystemMenu 角色信息
     * @return 操作结果
     */
    @ApiOperation(value = "系统菜单信息新增接口", notes = "系统菜单信息新增接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_MENU, operateDesc = "系统菜单新增")
    @PreAuthorize(value = "hasAuthority('system:menu:add')")
    @PostMapping("/add")
    public Result<FySystemMenu> add(@RequestBody FySystemMenu fySystemMenu) {
        return fySystemMenuService.add(fySystemMenu);
    }

    /**
     * 系统菜单信息编辑接口
     *
     * @param fySystemMenu 菜单信息
     * @return 操作结果
     */
    @ApiOperation(value = "系统菜单信息编辑接口", notes = "系统菜单信息编辑接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_MENU, operateDesc = "系统菜单编辑")
    @PreAuthorize(value = "hasAuthority('system:menu:update')")
    @PostMapping("/update")
    public Result<FySystemMenu> update(@RequestBody FySystemMenu fySystemMenu) {
        return fySystemMenuService.update(fySystemMenu);
    }

    /**
     * 系统菜单状态编辑接口
     *
     * @param systemMenuStatusUpdateVo 状态信息
     * @return 操作结果
     */
    @ApiOperation(value = "系统菜单状态编辑接口", notes = "系统菜单状态编辑接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_MENU, operateDesc = "系统菜单修改状态")
    @PreAuthorize(value = "hasAuthority('system:menu:update:status')")
    @PostMapping("/update/status")
    public Result<FySystemMenu> updateStatus(@RequestBody SystemMenuStatusUpdateVo systemMenuStatusUpdateVo) {
        return fySystemMenuService.updateStatus(systemMenuStatusUpdateVo);
    }

    /**
     * 系统菜单信息删除接口
     *
     * @param menuId 菜单ID
     * @return 操作结果
     */
    @ApiOperation(value = "系统菜单信息删除接口", notes = "系统菜单信息删除接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", required = true, value = "菜单ID", paramType = "query", dataType = "Long")
    })
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_MENU, operateDesc = "系统菜单删除")
    @PreAuthorize(value = "hasAuthority('system:menu:delete')")
    @PostMapping("/delete")
    public Result<String> delete(Long menuId) {
        return fySystemMenuService.delete(menuId);
    }

    /**
     * 根据角色ID列表获取菜单列表接口
     *
     * @param roleIds 角色ID列表
     * @return 菜单树
     */
    @ApiOperation(value = "根据角色ID列表获取菜单列表接口", notes = "根据角色ID列表获取菜单列表接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleIds", required = true, value = "角色ID列表", paramType = "query", dataType = "String")
    })
    @GetMapping("/tree")
    public Result<List<MenuTree>> getMenuTree(@RequestParam(value = "roleIds") String roleIds) {
        return fySystemMenuService.getMenuTreeByRoleIds(roleIds);
    }

    /**
     * 获取可用菜单树列表接口
     *
     * @return 菜单树列表
     */
    @ApiOperation(value = "获取可用菜单树列表接口", notes = "获取可用菜单树列表接口", httpMethod = "GET")
    @GetMapping("/usable/tree")
    public Result<List<MenuTree>> getAllUsableMenuTree() {
        return fySystemMenuService.getAllUsableMenuTree();
    }

    /**
     * 获取菜单树列表接口
     *
     * @return 菜单树列表
     */
    @ApiOperation(value = "获取菜单树列表接口", notes = "获取菜单树列表接口", httpMethod = "GET")
    @GetMapping("/tree/list")
    public Result<List<MenuTree>> getMenuTree() {
        return fySystemMenuService.getMenuTree();
    }

    /**
     * 获取可用菜单列表接口
     *
     * @return 菜单列表
     */
    @ApiOperation(value = "获取可用菜单列表接口", notes = "获取可用菜单列表接口", httpMethod = "GET")
    @GetMapping("/usable/list")
    public Result<List<FySystemMenu>> getAllUsableMenuList() {
        return fySystemMenuService.getAllUsableMenuList();
    }

    /**
     * 系统菜单编码校验接口
     *
     * @param menuCode 菜单编码
     * @param menuId   菜单ID
     * @return 校验结果
     */
    @ApiOperation(value = "系统菜单编码校验接口", notes = "系统菜单编码校验接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuCode", required = true, value = "菜单编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "menuId", required = true, value = "菜单ID", paramType = "query", dataType = "Long")
    })
    @GetMapping("/code/check")
    public Result<String> menuCodeCheck(String menuCode, Long menuId) {
        return fySystemMenuService.menuCodeCheck(menuCode, menuId);
    }

}
